/*
 * @lc app=leetcode.cn id=103 lang=cpp
 *
 * [103] 二叉树的锯齿形层序遍历
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>

using namespace std;
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> ret;
        if(!root) {
            return ret;
        }
        q.push(root);
        while(!q.empty()) {
            int current = q.size();
            ret.push_back(vector<int> ());
            for(int i = 1;i <= current;i++) {
                auto node = q.front(); 
                q.pop();
                ret.back().push_back(node->val);
                if (node->left) {
                    q.push(node->left);
                }
                if (node->right) {
                    q.push(node->right);
                } 
            }  
        }
        for(int i = 0;i < ret.size();i++) {
            if(i % 2 == 1) {
                reverse(ret[i].begin(),ret[i].end());
            }
        }
        return ret;
    }
};
// @lc code=end

